package main

import (
	"flag"
	"io"

	"gitee.com/sjy/selfSendfile/server/listener"
	"gitee.com/sjy/selfSendfile/server/service"
	"github.com/astaxie/beego/logs"
)

func main() {
	var tcpport string
	flag.StringVar(&tcpport, "tcp", ":44", "tcp port")
	flag.Parse()

	// 创建tcp监听
	tcp_listener := listener.BuildListener(tcpport)
	defer tcp_listener.Close()

	logs.Info("server start at %s", tcpport)
	// 创建服务容器
	ms := service.NewServer()

	ms.Server(func() {
		task(tcp_listener)
	})

	ms.Wait()
}

func task(listener listener.Listener) {
	defer func() {
		if err := recover(); err != nil && err != io.EOF {
			logs.Error("%v", err)
		}
	}()

	conn := listener.Accept()
	go HandleTcpConn(conn)
}
